package cronjob

import (
	"context"
	"github.com/jinzhu/copier"
	"go-zero-admin/application/job/rpc/pb"

	"go-zero-admin/application/job/api/internal/svc"
	"go-zero-admin/application/job/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReloadCronLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 重启定时任务
func NewReloadCronLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReloadCronLogic {
	return &ReloadCronLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ReloadCronLogic) ReloadCron(req *types.ReloadCronReq) (resp *types.ReloadCronRes, err error) {

	res, err := l.svcCtx.JobCronSvc.ReloadCron(l.ctx, &pb.ReloadScheduleReq{})
	if err != nil {
		return nil, err
	}
	resp = &types.ReloadCronRes{}
	_ = copier.Copy(resp, res)

	return resp, nil
}
